package com.xyj.zjbz.user.service.impl;

import com.xyj.zjbz.po.Role;
import com.xyj.zjbz.po.User;
import com.xyj.zjbz.po.ZJDataSource;
import com.xyj.zjbz.user.dao.UserDAO;
import com.xyj.zjbz.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDAO userDAO;

	public Map<String, Object> validUser(User user) {
		Map<String, Object> map = new HashMap<String, Object>();
		User u1 = userDAO.getUserByLoginName(user.getLoginName());
		if (u1 == null) {
			map.put("status", "-1");// 用户名不存在
		} else {
			User u = userDAO.validUser(user);
			if (u == null) {
				map.put("status", "-2");// 密码错误
			} else {
				map.put("status", "1");// 用户名和密码正确
				map.put("user", u);
			}
		}
        return map;
	}


	/**
	 * 获取用户对应的数据库数据源
	 */
	public ZJDataSource getBZSystemById(Integer sysId) {
		return userDAO.getDataSourceById(sysId);
	}

	public List<Role> getRolesByUser(User user) {
		return userDAO.getRoleByUserId(user.getId());
	}


	public List<ZJDataSource> getBZSystem() {
		return userDAO.getAllDataSource();
	}


	public Map<String, Object> addDataSource(ZJDataSource bzSystem) {
		userDAO.addDataSource(bzSystem);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", 1);
		return map;
	}

}
